<template>
  <div id="name">
    <div class="wl-body">
      <div class="top"></div>
      <div class="topbox slideOut">
        <div class="bdds">
          <div class="link-item">
            <div class="zc-dj t-left">TROT</div>
            <div class="slink defcf">
              <div class="flex1">{{ $t("m.ky") }}</div>
              <div class="flex1">{{ $t("m.zh") }}({{ bzdw }})</div>
            </div>
            <div class="slink u-dj">
              <div class="flex1">
                {{ see ? (s ? s.ftAmount.toFixed(4) : "0") : "*****" }}
              </div>

              <div class="flex1">
                {{ see ? (s ? (s.ftAmount * ttoc).toFixed(2) : "0") : "*****" }}
              </div>
            </div>
          </div>
        </div>
      </div>
      <div>
        <div class="i-title t-left mtp">— {{ $t("m.cwjl") }} —</div>
        <div
          class="mtp slink w30 xuankuang"
        >
          <div
            class="xk1l scllow"
            :class="[acchange == 0 ? 'active' : '']"
            @click="changers(0)"
          >
            {{ $t("m.qb") }}
          </div>
          <div
            class="xk1 scllow"
            :class="[acchange == 1 ? 'active' : '']"
            @click="changers(1)"
          >
            {{ $t("m.cb") }}
          </div>
          <div
            class="xk1r scllow"
            :class="[acchange == 2 ? 'active' : '']"
            @click="changers(2)"
          >
            {{ $t("m.tb") }}
          </div>
          <!-- <div class="xk1" :class="[acchange==3?'active':'']" @click="changers(3)">闪兑</div> -->
        </div>
        <div class="bdds slideIn czlist">
          <van-list
            v-model="loading"
            :finished="finished"
            :finished-text="$t('m.nomore')"
            @load="onLoad"
          >
            <div class="link-item bottml" v-for="(item, k) in lslist" :key="k">
              <div class="zc-dj t-left">
                {{
                  item.state == 1
                    ? $t("m.cb")
                    : item.state == 2
                    ? $t("m.tb")
                    : item.state == 3
                    ? $t("m.sd")
                    : ""
                }}
              </div>
              <div class="slink defcf">
                <div class="flex1">{{ $t("m.sl") }}</div>
                <div class="flex1">{{ $t("m.zt") }}</div>
                <div class="flex1">{{ $t("m.sj") }}</div>
              </div>
              <div class="slink u-dj">
                <div class="flex1">{{ item.amount }}</div>
                <div class="flex1">
                  {{
                    item.status == 1
                      ? $t("m.qkqrz")
                      : item.status == 2
                      ? $t("m.ywc")
                      : item.status == 3
                      ? $t("m.qkqrsb")
                      : ""
                  }}
                </div>
                <div class="flex1">{{ item.createTime }}</div>
              </div>
            </div>
          </van-list>
        </div>
      </div>

      <div class="bottomflex flesa">
        <div class="sxdq mtp" @click="congbi()">
          <img src="../assets/timg/cbbtn.png" alt="" class="iixo" />
          <div>
            {{ $t("m.cb") }}
          </div>
        </div>
        <div class="sxdq mtp" @click="tibi()">
          <img src="../assets/timg/tbbtn.png" alt="" class="iixo" />
          <div>
            {{ $t("m.tb") }}
          </div>
        </div>
        <div class="sxdq mtp" @click="goto('bzdh')">
          <img src="../assets/timg/sdbtn.png" alt="" class="iixo" />
          <div>
            {{ $t("m.sd") }}
          </div>
        </div>
      </div>
    </div>
    <van-popup v-model="cbshow" position="bottom">
      <div class="tbdlog">
        <div class="sdadw">
          <div class="txte">{{ $t("m.cb") }}</div>
          <div>
            <div class="tts">TROT</div>
            <div class="isde">
              <input
                class="inputs"
                type="number"
                v-model="nums"
                :placeholder="$t('m.zxtbsl') + min"
                @keydown="gs"
              />
            </div>
                 <div class="isdesd nos">
             <div>{{ $t("m.kcz") }}:</div>
              <div> {{unums!=null?unums:'***'}} TROT</div>
            </div>
          </div>
        </div>
        <div class="bnts" @click="yqmao()">{{ $t("m.cb") }}</div>
      </div>
    </van-popup>
  
    <van-popup v-model="tbshow" position="bottom">
      <div class="tbdlog">
        <div class="sdadw">
          <div class="txte">{{ $t("m.tb") }}</div>
          <div>
            <div class="tts">TROT</div>
            <div class="isdesd bottmb">
              <div>
                <input
                type="number"
                  class="inputs"
                  v-model="tbnums"
                  :placeholder="$t('m.zxtbsl') + min"
                  @keydown="gs"
                />
              </div>
              <div>
                 <span @click="all()">{{ $t("m.qb") }}</span>
              </div>
            </div>
            <div class="isdesd ">
              <div>{{ $t("m.ky") }}:</div>
              <div>{{ s? s.ftAmount.toFixed(4):'0' }}TROT</div>
            </div>
            <div class="isdesd">
              <div>{{ $t("m.kgf") }}:</div>
              <div>{{sxf}}TROT/{{$t('m.bi')}}</div>
            </div>
            <div class="isde">
              <div>{{ $t("m.dzsl") }}</div>
              <div>{{ tbnums ? (tbnums-sxf).toFixed(4) : 0 }} TROT</div>
            </div>
          </div>
        </div>
        <div class="bnts" @click="yqmaos()">{{ $t("m.tb") }}</div>
      </div>
    </van-popup>
  </div>
</template>

<script>
import { recharge, tokenBalance, getSign } from "../api/tron";
export default {
  watch: {
  },
  name: "name",
  data() {
    return {
      isPublish: false,
      acchange: 0,
      cbshow: false,
      tbshow: false,
      show: true,
      see: "",
      lslist: [],
      loading: false,
      finished: false,
      pageNum: 1,
      nums: "",
      unums: "",
      suoding: false,
      tbnums: "",
      tbsding: false,
      bzdw: "CNY",
      gsb: "",
      sxf: "",
      min:''
    };
  },

  async created() {
    this.gb()
    if (this.$store.getters.language == "en-US") {
      this.bzdw = "USD";
    } else {
      this.bzdw = "CNY";
    }

    if (this.$store.getters.see == "0") {
      this.see = false;
    } else {
      this.see = true;
    }
  },

  computed:{
       ttoc() {
      return this.$store.getters.ttoc;
    }, 
       s(){return this.$store.getters.s}


  },
  methods: {
       gs(e){
        e.target.value = (e.target.value.match(/^\d*(\.?\d{0,5})/g)[0]) || null
      },
   gb() {
        let datas = {
          params: {
            pageNum: 1,
            pageSize: 10
          }
        }
        this.$get({
          url: 'sms-base/getList',
          data: datas,
          callback: data => {
            this.$nextTick(function () {
              if (data.data) {
                this.gsb = data.data[0]
                this.min=this.gsb.minT
                this.sxf=this.gsb.feeT

              }

            });
          }
        })
      },

    guanbi() {
      this.$router.go(-1);
    },
  
    changers(val) {
      this.acchange = val;
      this.show = false;
      this.lslist = [];
      this.pageNum = 1;
      this.onLoad();
    },
    congbi() {
       this.cbshow = true;  
        this.getye()
    },
       async getye() {
       await tokenBalance("TROT")
        .then((res) => {
          this.unums = res;         
        })
        .catch(() => {
          this.getye()
        });
  },
    yqmao() {
   if(this.nums==0){
      this.$toast(this.$i18n.t('m.dhslshr'));
        return
      }
      if (this.nums > this.unums) {
         this.$toast(this.$i18n.t('m.qbyebz'));
        this.nums = this.unms;
        return;
      }
      if (this.suoding) {
        return;
      } else {
        this.suoding = true;
        this.$toast.loading();
        recharge("TROT", this.nums).then(
          (res) => {
         
            if(res){             
               this.$toast(this.$i18n.t("m.messageerrors"));
            }else{
             this.getdata();
            }
            this.suoding=false
          },
          (err) => {
            // 交易失败
            this.suoding=false
            this.$toast(err);
          }
        );
      }
    },
    tibi() {
      this.tbshow = true;
    },
    all() {
      this.tbnums = this.s.ftAmount;
    },
    yqmaos() {
        if(!this.sxf){
           this.$toast(this.$i18n.t('m.sjzjz'));
        return
      }
      if(this.tbnums==0){
          this.$toast(this.$i18n.t('m.dhslshr'));
        return
      }
    if(this.s.ftAmount-this.tbnums<0){
      this.tbnums=this.s.ftAmount
      this.$toast(this.$i18n.t('m.qbyebz'));
        return
    }
        if(this.tbnums-this.sxf<=0){
          this.$toast(this.$i18n.t('m.dhslsh'));
        return
      }
          if (this.tbnums-this.min<=0) {
        this.$toast(this.$i18n.t('m.zxtbsl')+ this.min);
        return;
      }
      if (this.gsb.transferState == 1) {
        this.$toast(this.$i18n.t("m.qukuaigengxin"));
        return;
      } else {
        if (this.tbnums<=0 || this.tbsding) {
          this.$toast(this.$i18n.t("m.messageerror"));
          return;
        } else {
       this.$dialog.confirm({
  title: this.$i18n.t('m.txt'),
  message:this.$i18n.t('m.tb')+(this.tbnums-this.sxf) +'TROT',
    confirmButtonText: this.$i18n.t('m.qding'), 
   cancelButtonText: this.$i18n.t('m.gb') 
})
  .then(() => {

getSign(this.$store.getters.userWallet).then(

  (res)=>{
        if(res){        
          this.tbsding = true;
          let datas = {
            sendAddress: this.$store.getters.userWallet,
            sendId: this.$store.getters.nameid,
            receiveAddress: this.$store.getters.userWallet,
            state: 2,
            amount:  parseFloat(this.tbnums).toFixed(4) ,
            type: 2,
          };
          this.$post({
            url: "wms-wallet/transferWallet",
            data: datas,
            callback: (data) => {
              this.$nextTick(function () {
                if (data.data) { 
                  if (this.$store.getters.language == "zh-CN") {
                    this.$toast(data.message);
                  } else {
                    this.$toast(this.$i18n.t("m.messagesuccess"));
                  }                
                  this.tbnums = "";
                  this.getdata();
                  this.tbshow = false;
                  setTimeout(() => {
                    this.tbsding = false;
                  }, 2000);
                } else {
                  if (this.$store.getters.language == "zh-CN") {
                    this.$toast(data.message);
                  } else {
                    this.$toast(this.$i18n.t("m.messageerror"));
                  }
                  setTimeout(() => {
                    this.tbsding = false;
                  }, 2000);
                }
              });
            },
          });
          
        }else{
           this.$toast(this.$i18n.t("m.messageerrors"));
           this.tbsding = false;
           this.tbnums = "";
        }
},
()=>{
   this.$toast(this.$i18n.t("m.messageerrors"));
}
)
  })
        }
      }
    },
    getdata() {
      let datas = {
        params: {
          address: this.$store.getters.userWallet,
        },
      };
      this.$get({
        url: "wms-wallet/getWallet",
        data: datas,
        callback: (data) => {
          this.$nextTick(function () {
            this.s = data.data;
            if (this.s) {
              this.$toast(this.$i18n.t('m.messagesuccess'));
              this.cbshow = false;
              this.nums=''
            }
          });
        },
      }).catch(() => {
        this.$toast(this.$i18n.t("m.errordata"));
      });
    },
    goto(val) {
      this.$router.push({ path: val });
    },
    onLoad() {
      if (this.$store.getters.nameid) {
        var datas = {
          params: {
            uid: this.$store.getters.nameid,
            pageSize: 10,
            pageNum: this.pageNum,
            type: 2,
            state: this.acchange,
          },
        };
        this.$get({
          url: "wms-wallet/getHistory",
          data: datas,
          callback: (data) => {
            this.$nextTick(function () {
              var list = data.data;
              if (list) {
                this.show = true;
                if (this.pageNum > 1) {
                  this.lslist = [...this.lslist, ...list];
                } else {
                  this.lslist = list;
                }
                this.loading = false;
                if (list.length != 0) {
                  this.pageNum++;
                } else {
                  this.finished = true;
                }
              }
            });
          },
        }).catch(() => {
          this.$toast(this.$i18n.t("m.errordata"));
          
        });
      } else {
        this.getuid();
      }
    },
           getuid(){
            this.$get({
        url: "wms-wallet/user-game-info/" + this.$store.getters.userWallet,
        data: {},
        callback: (data) => {
          this.$nextTick(function () {          
            if(data.data){    
            this.$store.commit("nameid", data.data.id); 
            this.onLoad()       
             }
          });
        },
      }).catch(() => {
        this.$toast(this.$i18n.t("m.errordata"));
      });
        },
  },
};
</script>

<style lang="scss" scoped>
</style>